// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="XamlmNinja">
//   2010
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ReplayLastMessageMessenger.ViewModel
{
    using System;
    using System.Threading;

    using GalaSoft.MvvmLight.Messaging;

    using Model;
    using Messages;

    using WP7Contrib.Collections;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    /// <summary>
    /// The main view model.
    /// </summary>
    public class MainViewModel : ViewModelBaseWp7
    {
        #region Constants and Fields

        /// <summary>
        /// The currently selected person.
        /// </summary>
        private Person currentlySelectedPerson;

        /// <summary>
        /// The persons.
        /// </summary>
        private ObservableCollection<Person> persons;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        public MainViewModel(INavigationService navigationService, IMessenger messenger, ILog log)
            : base(navigationService, messenger, log)
        {
            // this is just for the sample :)
            this.Persons.Add(
                new Person
                    {
                        FirstName = "Richard",
                        LastName = "Griffin",
                        ProfileImage = "../Resources/Images/RichGriffin.png"
                    });
            this.Persons.Add(new Person
            {
                FirstName = "Ollie",
                LastName = "Riches",
                ProfileImage = "../Resources/Images/OllieRiches.png"
            });
            this.Persons.Add(new Person
            {
                FirstName = "Laurent",
                LastName = "Bugnion",
                ProfileImage = "../Resources/Images/LaurentBugnion.png"
            });
            this.Persons.Add(new Person
            {
                FirstName = "Mike",
                LastName = "Wolf",
                ProfileImage = "../Resources/Images/MikeWolf.png"
            });

            ThreadPool.QueueUserWorkItem(state =>
            {
                // messaging
                this.MessengerInstance.Register<NotificationMessageAction<Person>>(
                    this, message => message.Execute(this.CurrentlySelectedPerson));
            });
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets CurrentlySelectedCurrentlySelectedPerson.
        /// </summary>
        public Person CurrentlySelectedPerson
        {
            get
            {
                return this.currentlySelectedPerson;
            }

            set
            {
                this.SetPropertyAndNotify(ref this.currentlySelectedPerson, value, "CurrentlySelectedPerson");
                this.NavigationService.Navigate(new Uri("/View/ChildPage.xaml", UriKind.Relative));
                this.MessengerInstance.Send(new SelectedPersonMessage(this.currentlySelectedPerson));
            }
        }

        /// <summary>
        /// Gets Persons.
        /// </summary>
        public ObservableCollection<Person> Persons
        {
            get
            {
                if (this.persons == null)
                {
                    this.persons = new ObservableCollection<Person>();
                }

                return this.persons;
            }

            private set
            {
                this.SetPropertyAndNotify(ref this.persons, value, "Persons");
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The is being activated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingActivated(IStorage storage)
        {
        }

        /// <summary>
        /// The is being deactivated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingDeactivated(IStorage storage)
        {
        }

        #endregion
    }
}